package com.my;

import java.io.IOException;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Before;

public class Test {
	
	public static void main(String[] args) {
		try {
			Test t = new Test();
			t.b();
			
			//t.zk.setData("/GroupMembers/leader", "ee".getBytes(), 0);
			String ZK_PATH = "/GroupMembers2";
			t.zk.exists( "/GroupMembers2", true );
			t.zk.create("/GroupMembers2", "ca".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 
			
			t.zk.getData(ZK_PATH, true, null);
			//读取子节点
			t.zk.getChildren( ZK_PATH, true );
			
			//t.zk.setData("/GroupMembers2", "bbcc".getBytes(), -1);
			
			//t.zk.delete(ZK_PATH, -1);
			
			//
			
			/*t.zk.exists( "/GroupMembers" + "/leader333", true );
			t.zk.create("/GroupMembers" + "/leader333", "ca".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); 
			
			*/
			
			
			
			/*System.out.println(new String(t.zk.getData("/", false, null)));
			
			t.zk.setData("/", "ff".getBytes(), 1);
			
			t.zk.exists("/", true);
			
			Stat s = t.zk.exists("/testRootPath", new Watcher(){

				@Override
				public void process(WatchedEvent event) {
					System.out.println("++++++" + event);
				}
				
			});
			
			System.out.println(s);*/
			
			//t.zk.create("/testRootPatha", "testRootData".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			
			// 创建一个子目录节点
			//t.zk.create("/testRootPath/testChildPathOne", "testChildDataOne".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			
			Thread.currentThread().join();
			
			//t.test();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	ZooKeeper zk;
	
	@Before
	public void b() {
		try {
			zk = new ZooKeeper("localhost:3181", 10000, new Watcher() {
				// 监控所有被触发的事件
				public void process(WatchedEvent event) {
					System.out.println("已经触发了" + event.getType() + "事件！");
					
					if(EventType.NodeDataChanged == event.getType() ) {
						try {
							System.out.println(new String(zk.getData("/GroupMembers2", true, null)));
						} catch (KeeperException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			});
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@org.junit.Test
	public void test2() {
		try {
			zk.setData("/testRootPath", new String("fuck").getBytes(), 0);
		} catch (KeeperException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@org.junit.Test
	public void test() throws Exception {
		// 创建一个与服务器的连接
		
		// 创建一个目录节点
		//zk.create("/testRootPath", "testRootData".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		
		// 创建一个子目录节点
		//zk.create("/testRootPath/testChildPathOne", "testChildDataOne".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		System.out.println(new String(zk.getData("/", false, null)));
		System.out.println(new String(zk.getData("/testRootPath", false, null)));
		// 取出子目录节点列表
		System.out.println(zk.getChildren("/testRootPath", true));
		// 修改子目录节点数据
		//zk.setData("/testRootPath/testChildPathOne", "modifyChildDataOne".getBytes(), -1);
		//System.out.println("目录节点状态：[" + zk.exists("/testRootPath", true) + "]");
		// 创建另外一个子目录节点
		//zk.create("/testRootPath/testChildPathTwo", "testChildDataTwo".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		System.out.println(new String(zk.getData("/testRootPath/testChildPathTwo", true, null)));
		// 删除子目录节点
		//zk.delete("/testRootPath/testChildPathTwo", -1);
		//zk.delete("/testRootPath/testChildPathOne", -1);
		// 删除父目录节点
		//zk.delete("/testRootPath", -1);
		// 关闭连接
		//zk.close();
	}

}
